
package com.complex.drools;
import java.io.FileReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.event.DebugAgendaEventListener;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.rule.Package;

/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class HelloWorldExample {

public static void main(String[] args) throws Exception {
   String filepath = "D:/work/project/own/complex/complex.drools/src/main/resources/HelloWorld.drl";
   final Reader source = new FileReader(filepath);
   final PackageBuilder builder = new PackageBuilder();
  
   // this will parse and compile in one step
   builder.addPackageFromDrl(source);

   // Check the builder for errors
   if (builder.hasErrors()) {
    System.out.println(builder.getErrors().toString());
    throw new RuntimeException("Unable to compile \"HelloWorld.drl\".");
   }

   // get the compiled package (which is serializable)
   final Package pkg = builder.getPackage();

   // add the package to a rulebase (deploy the rule package).
   final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
   ruleBase.addPackage(pkg);

   final StatefulSession session = ruleBase.newStatefulSession();

   session.addEventListener(new DebugAgendaEventListener());
   session.addEventListener(new DebugWorkingMemoryEventListener());

final Message message = new Message();
   message.setMessage("Hello World");
   message.setStatus(Message.HELLO);
   session.insert(message);

   session.fireAllRules();

session.dispose();
}

public static class Message {
   public static final int HELLO = 0;
   public static final int GOODBYE = 1;

   private String message;

   private int status;

   public Message() {

   }

   public String getMessage() {
    return this.message;
   }

   public void setMessage(final String message) {
    this.message = message;
   }

   public int getStatus() {
    return this.status;
   }

   public void setStatus(final int status) {
    this.status = status;
   }
}

}